/*
 * @Descripttion: 护士登录
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-05 20:46:47
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-06 12:29:56
 */

const DB = require('../../lib/DB');
const { NURSE } = require('../../lib/Identity');
const { Success, Fail } = require('../../lib/Response');

module.exports = async function (ctx, next) {
    try {
        const { name, pwd } = ctx.request.body;
        // identity 显示其必须是护士身份
        const nurse = await DB.models.user.findOne({ where: { name, pwd, identity: NURSE } })
        if (nurse == null) {
            throw new Error("用户不存在或密码错误")
        }
        nurse.login_time = Date.now();
        await nurse.save();
        ctx.session.nurse = nurse.toJSON();
        ctx.body = new Success(nurse.toJSON())
        await next();
    } catch (e) {
        ctx.body = new Fail(e.message)
    }
}